/* Copyright 2014 Nate Eisner, Noah Bahr
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.uwp.kcwm;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Info extends Activity {

	Intent intent;
	private ImageView infoImage;
	TextView infoText;
	String exhibitNum;
	Bitmap bitmap;
	WebView wView;
	// URL for webserver
	private static final String SITEURL = "http://gracefullyengineered.com/kcwm/";
	private ProgressDialog pDialog;

	/** Called when the activity is first created. 
	 *  Loads picture and information for the exhibit
	 * */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info);
		infoImage = (ImageView) findViewById(R.id.exhibitInfoPic);
		pDialog = new ProgressDialog(this);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			exhibitNum = extras.getString("exhibitNum");
		}
		getActionBar().setTitle("Exhibit " + exhibitNum + " - Information");

		new LoadImg().execute(SITEURL + exhibitNum + "/exhibit.jpg");

		wView = (WebView) findViewById(R.id.exhibitInfo);
		wView.loadUrl(SITEURL + exhibitNum + "/info.html");
	}

	/**
	 * On activity resume reload webview content
	 */
	@Override
	public void onResume() {
		super.onResume();
		wView.loadUrl(SITEURL + exhibitNum + "/info.html");
	}

	/**
	 * Loads picture from url and assigns to image views
	 *
	 */
	class LoadImg extends AsyncTask<String, Void, Bitmap> {
		/**
		 * Starts progress dialog
		 */
		protected void onPreExecute() {
			pDialog.setMessage("Loading Exhibit " + exhibitNum
					+ " information...");
			pDialog.show();
		}
		/**
		 *Gets picture from url and loads into a bitmap arraylist
		 */
		@Override
		protected Bitmap doInBackground(String... urls) {
			try {
				bitmap = BitmapFactory.decodeStream((InputStream) new URL(
						urls[0]).getContent());
			} catch (Exception e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return bitmap;
		}

		/**
		 * Assigns bitmap to image view
		 */
		@Override
		protected void onPostExecute(Bitmap result) {

			if (result != null) {
				infoImage.setImageBitmap(result);
			} else {
				Toast.makeText(Info.this,
						"Image does not exist or Network Error",
						Toast.LENGTH_SHORT).show();
			}
			pDialog.dismiss();
		}
	}
}
